package dbb

import (
	"context"

	_ "unsafe"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/widget"
	_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/text/gstr"
	"github.com/y2h/fyne_poem/menu"
	"github.com/y2h/fyne_poem/util"
	"xorm.io/xorm"

	// _ "modernc.org/sqlite"
	_ "github.com/mattn/go-sqlite3"
)

// 调用其它包中的私有函数、全局变量
// https://blog.csdn.net/gold0523/article/details/94341008

//go:linkname tableFieldsMap github.com/gogf/gf/contrib/drivers/sqlite/v2.tableFieldsMap
var tableFieldsMap *gmap.AnyAnyMap
var (
	// 数据库名称
	dbs                           = gmap.NewStrAnyMap()
	sqlite_master                 = "sqlite_master"
	Ctx           context.Context = context.Background()

	rsData gdb.Result
	bdddl  binding.String

	// 数据表格容器
	CtnData *fyne.Container

	// 数据表格表头即列名容器
	CtnHeader *fyne.Container
	lstData   *widget.List

	// 数据表记录总数
	bdTotal binding.Int
	page    util.Page

	pageSize    = 10
	currentPage = 1
)

func gf2xorm(db gdb.DB) *xorm.Engine {
	driverName := ""
	if gstr.Contains(db.GetConfig().Type, "sqlite") {
		driverName = "sqlite3"
	} else {
		driverName = db.GetConfig().Type
	}
	// println(driverName)
	// println("link  ",db.GetConfig().Link)
	engine, err := xorm.NewEngine(driverName,
		db.GetConfig().Link)
	if err != nil {
		msg(err.Error())
		return nil
	}
	menu.Glog.Error(Ctx, err)
	return engine
}
